package nayasis.common.db.manager.sql;

import java.util.Map;
import java.util.Stack;
import java.util.StringTokenizer;

public class XmlSqlConditionChecker {

    Stack<String> stack = new Stack();
    
    private enum Command {

        equal("eq"), 
        notEqual("notEq"),
        empty("empty"),
        notEmpty("notEmpty"),
        greaterThan("gt"),
        greaterThanOrEqual("ge"),
        lessThan("lt"),
        lessThanOrEqual("le"),
        
        and("and"),
        or("or"),
        
        zzz("noteq");
        
        private String command;
        
        Command( String command ) {
            this.command = command;
        }
        
        public String toString() {
            return this.command;
        }
        
        
    }
    
    
    public boolean isTrue( String condition, Map<String, ParamValue> bindingParam ) {
        
        System.out.println( condition );
        System.out.println( parseBrace(condition) );
        
        return true;
    }

    private Stack<String> parseBrace( String condition ) {
        
        Stack<Integer> stackBrace   = new Stack<>();
        Stack<String>  stackCommand = new Stack<>();
        
        StringBuffer sb = new StringBuffer();
        sb.append( '(' ).append( condition ).append( ')' );
        
        for( int i = 0, iCnt = sb.length() - 1; i < iCnt; i++ ) {
            
            char c = sb.charAt( i );
            
            if( isStartBrace( c ) ) {
                stackBrace.push( i );
                continue;
            }

            if( isEndBrace( c ) ) {
                
                int startIndex = stackBrace.pop();
                
                if( i - startIndex == 1 ) continue;
                
                stackCommand.push( sb.substring( startIndex + 1, i ) );
                
            }
            
        }
        
        return stackCommand;
        
    }

    private boolean isEndBrace( char c ) {
        return c == ')' || c == ']' || c == '}';
    }

    private boolean isStartBrace( char c ) {
        return c == '(' || c == '[' || c == '{';
    }
    
    
    
    
}
